package com.qf.controller;

import com.qf.pojo.Fangxing;
import com.qf.service.FangxingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/fangxing")
public class FangxingController {
    @Autowired
    private FangxingService fangxingService;

    /*   传入酒店ID查房型，且房型数>0 返回list<fangxing>*/
    @RequestMapping("/selectFangxingByJid/{jid}")
    @ResponseBody
    public List<Fangxing> selectFangxingByJid(@PathVariable Integer jid) {
        List<Fangxing> fangxings = fangxingService.selectFangxingByJid(jid);
        return fangxings;
    }
/*------------------------后台------------------------*/
@RequestMapping("/selectAll")
public String selectAll(Model model) {
    List<Fangxing> fangxings = fangxingService.selectAll();
    model.addAttribute("fangxinglists",fangxings);
    return "fangxinglist";
}

    //根据房型id删除房型，成功返回到success页面
    @RequestMapping("/deleteFangxingByFid/{fid}")
    @ResponseBody
    public String deleteFangxingByFid(@PathVariable Integer fid) {
        fangxingService.deleteFangxingByFid(fid);
        return "success";
    }

    /*传房型对象，添加房型，返回到success页面*/
    @RequestMapping("/addFangxing")
    public String addFangxing(Fangxing fangxing) {
        fangxingService.addFangxing(fangxing);
        return "success";
    }

    //修改房型，先显示，到修改页面
    @RequestMapping("/updateFangxingByFid/{fid}")
    public String updateFangxingByFid(@PathVariable Integer fid, Model model) {
//        根据房型的ID查房型
        Fangxing fangxing = fangxingService.selectFangxingByFid(fid);
        model.addAttribute("fangxing", fangxing);
        return "updatefangxing";/*跳到房型修改页面*/
    }

    //    在修改页面修改后，提交到重定向到房型列表
    @RequestMapping("/updateFangxing")
    public String updateFangxing(Fangxing fangxing) {
        /*修改的方法*/
        fangxingService.updateFangxing(fangxing);
        return "redirect:/fangxing/selectAll";/*房型的列表*/
    }
}
